package org.apereo.cas.Test;

import org.apereo.cas.entity.User;

import java.sql.*;

/**
 * 待废弃，jdbc没意义
 */
public class UserDaoTest {

    public static void main(String[] args) {
        UserDaoTest userDaoTest = new UserDaoTest();
        User user = userDaoTest.getUser("admin");

        System.out.println(user);
    }

    public User getUser(String name){
        User user = new User();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            String driver = "com.mysql.cj.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/sso_test?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true\n";
            String username = "root";
            String password = "root";

            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);

//            String sql = "SELECT * FROM egin_sys_user WHERE login_name =?";
            String sql = "SELECT * FROM user WHERE username =?";

            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "admin");
            resultSet = preparedStatement.executeQuery();

            while(resultSet.next()){
//                String userPassword = resultSet.getString("user_password");
                String userPassword = resultSet.getString("password");
                user.setLoginName(name);
                user.setUserPassword(userPassword);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                if(resultSet != null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if(preparedStatement != null){
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  user;
    }
}